<?php
/**
 * User: aka
 * Date: 2018/6/24
 * Time: 15:04
 */

namespace app\common\db;

use think\Db;

class DbOperation
{
    private function getDb(string $dbName)
    {
        return Db::table($dbName);
    }

    public function select(string $dbName)
    {
        $result = $this->getDb($dbName)->select();
        return $result ? $result : false;
    }

    public function whereSelect(string $dbName,array $where)
    {
        $result = $this->getDb($dbName)->where($where)->select();
        return $result ? $result : false;
    }

    public function find(string $dbName,array $where)
    {
        $result = $this->getDb($dbName)->where($where)->find();
        return $result ? $result : false;
    }

    public function fieldFind(string $dbName,array $where,array $field)
    {
        $result = $this->getDb($dbName)->where($where)->field($field)->find();
        return $result ? $result : false;
    }

    public function add(string $dbName,array $addData)
    {
        $result = $this->getDb($dbName)->insert($addData);
        return $result ? true : false;
    }

    public function addGetId(string $dbName,array $addData)
    {
        $id = $this->getDb($dbName)->insertGetId($addData);
        return !empty($id) ? $id : '';
    }

    public function update(string $dbName,int $id,array $updateData)
    {
        $result = $this->getDb($dbName)->where('id',$id)->update($updateData);
        return $result ? true : false;
    }

    public function delete(string $dbName,array $where)
    {
        $result = $this->getDb($dbName)->where($where)->delete();
        return $result ? true : false;
    }

    public function paginate(string $dbName,int $num)
    {
        $result = $this->getDb($dbName)->paginate($num);
        return $result ? $result : false;
    }

    public function wherePaginate(string $dbName,array $where,int $num)
    {
        $result = $this->getDb($dbName)->where($where)->paginate($num);
        return $result ? $result : false;
    }
}